package com.ni.learning.exam.controller;

import com.ni.learning.exam.dto.AnswerInfoDTO;
import com.ni.learning.exam.service.AnswerSheetService;
import com.our.learning.infrastructure.constant.ResponseStatusEnum;
import com.our.learning.infrastructure.po.AnswerInfo;
import com.our.learning.infrastructure.pojo.Response;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author : Ice'Clean
 * @date : 2023-06-24
 */
@RestController
@RequestMapping("/sheet")
public class AnswerSheetController {

    private final AnswerSheetService answerSheetService;

    public AnswerSheetController(AnswerSheetService answerSheetService) {
        this.answerSheetService = answerSheetService;
    }

    /**
     * 获取学生错题 ID
     * @param studentId 学生 ID
     * @param paperId 试卷 ID（为 null 表示获取所有）
     * @return 错题 ID 列表
     */
    @GetMapping("/incorrect")
    public List<Integer> getIncorrectIds(int studentId, Integer paperId) {
        return answerSheetService.getIncorrectIds(studentId, paperId);
    }

    /**
     * 考试提交一题的答案
     * @param answerInfoDTO 答案信息
     * @return 提交结果
     */
    @PostMapping("/submit")
    public Response submitAnswer(AnswerInfoDTO answerInfoDTO) {
        return  answerSheetService.submitAnswer(answerInfoDTO);
    }

    /**
     * 获取学生答题卡上某一题的答案
     * @param answerInfoDTO 答案信息查询（sheetId 和 titleNo 生效）
     * @return 答案信息
     */
    @PostMapping("/answer")
    public Response getSheetAnswer(AnswerInfoDTO answerInfoDTO) {
        int sheetId = answerInfoDTO.getSheetId();
        int titleNo = answerInfoDTO.getTitleNo();
        AnswerInfo answerInfo = answerSheetService.getSheetAnswer(sheetId, titleNo);
        return new Response(ResponseStatusEnum.OK)
                .addData("empty", answerInfo == null)
                .addData("answerInfo", answerInfo);
    }

    /**
     * 教师提交评分
     * @param answerInfoDTO 答案于评分信息（sheetId、titleNo 和 score 生效）
     * @return 评分结果
     */
    @PostMapping("/check")
    public Response submitCheck(AnswerInfoDTO answerInfoDTO) {
        return answerSheetService.submitCheck(answerInfoDTO);
    }

    /**
     * 获取指定考试的所有答题卡
     * @param examId 考试 ID
     * @return 答题卡列表
     */
    @PostMapping("/list")
    public Response sheetList(int examId) {
        return answerSheetService.sheetList(examId);
    }

    /**
     * 教师提交批改结果
     * @param sheetId 答题卡 ID
     * @return 提交反馈
     */
    @PostMapping("/check/submit")
    public Response submitCheckResult(int sheetId) {
        return answerSheetService.submitCheckResult(sheetId);
    }
}
